package com.lsj.gulimall.product.app.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.lsj.gulimall.product.entity.BrandEntity;
import com.lsj.gulimall.product.service.BrandService;
import com.lsj.gulimall.product.vo.BrandVo;
import com.lsj.gulimall.product.vo.CategoryBrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lsj.gulimall.product.entity.CategoryBrandRelationEntity;
import com.lsj.gulimall.product.service.CategoryBrandRelationService;
import com.lsj.common.utils.PageUtils;
import com.lsj.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author lsj
 * @email 1326646663@qq.com
 * @date 2022-01-06 11:30:26
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/catelog/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params,Long brandId){
        PageUtils page = categoryBrandRelationService.queryPage(params,brandId);

        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandVo categoryBrandVo){
		categoryBrandRelationService.save(categoryBrandVo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 获取分类关联的品牌
     * @param catId
     * @return
     */
    @GetMapping("/brands/list")
    public R getCategroyRelationBrands(@RequestParam("catId") Long catId) {
        List<BrandEntity> brands = brandService.getCategroyRelationBrands(catId);
        List<BrandVo> brandVos = null;
        if (brands != null) {
            brandVos = brands.stream().map(brand -> {
                BrandVo brandVo = new BrandVo();
                brandVo.setBrandId(brand.getBrandId());
                brandVo.setBrandName(brand.getName());
                return brandVo;
            }).collect(Collectors.toList());
        }
        return R.ok().put("data",brandVos);
    }

}
